import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import tensorflow as tf


def fun_himmelblau(x):
    return (x[0] ** 2 + x[1] - 11) ** 2 + (x[0] + x[1] ** 2 - 7) ** 2


x = np.arange(-6, 6, 0.1)
y = np.arange(-6, 6, 0.1)
print('x,y range:', x.shape, y.shape)
X, Y = np.meshgrid(x, y)
print('X: ', X)
print('Y: ', Y)
print('X,Y maps:', X.shape, Y.shape)
Z = fun_himmelblau([X, Y])

fig = plt.figure('himmelblau function')
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
ax.view_init(60, -30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()

lr = 0.01
X = tf.constant([[4., 0.], [1., 0.], [-4, 0.], [4, 0.]])
for x in X:
    # x = tf.constant([-4., 0.])
    for step in range(200):
        with tf.GradientTape() as tape:
            tape.watch([x])
            y = fun_himmelblau(x)
        gradient = tape.gradient(y, [x])
        x = x - lr * gradient[0]
    print(x)
